<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%classroom_attr}}".
 *
 * @property integer $id
 * @property integer $classroom_id
 * @property string $classday
 * @property integer $classtime
 * @property string $start_time
 * @property string $end_time
 */
class ClassroomAttr extends \zhtfrontend\components\bases\BaseModel
{
    public $start_min;
    public $start_hour;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%classroom_attr}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['classday', 'classroom_id', 'arrange_class_id', 'status', 'created_at', 'updated_at', 'user_id'], 'integer'],
            [['classroom_id', 'start_time', 'end_time', 'classday'], 'required'],
            [['start_time', 'end_time',], 'check_time'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', 'ID'),
            'classroom_id' => Yii::t('zhtfrontend', '教室编号'),
            'classday' => Yii::t('zhtfrontend', '上课星期'),
            'arrange_class_id' => Yii::t('zhtfrontend', '排课id'),
            'status' => Yii::t('zhtfrontend', '状态'),
            'start_time' => Yii::t('zhtfrontend', '上课时间'),
            'end_time' => Yii::t('zhtfrontend', '下课时间'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'updated_at' => Yii::t('zhtfrontend', '修改时间'),
            'user_id' => Yii::t('zhtfrontend', '管理员'),
            'start_min' => Yii::t('zhtfrontend', '开始分钟'),
            'start_hour' => Yii::t('zhtfrontend', '开始小时'),
        ];
    }

    public function extraFields()
    {
        return [
            'classroom_name' => function () {
                return isset($this->classroom)?$this->classroom->name:'';
            },
            'classroom_number' => function () {
                return isset($this->classroom)?$this->classroom->room_number:'';
            },
            'user_name' => function () {
                return !empty($this->admin) ? $this->admin->username : '';
            },
            'zhtfrontend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    ['delete' => function ($data) {
                       $res =  Arrangement::find()->where(['classroom_attr_id'=>$data['id'],'is_delete'=>0])->one();
                        if ($res != null) {
                            return '<button class="yx-btn-disabled" type="button">删除</button>';
                        } else {
                            return '';
                        }
                    },
                        'update'=> function(){
                            return '';
                        },

                    ]
                )->create();
            },

        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['classday'] = function () {
            switch ($this->classday) {
                case 1:
                    return \Yii::t('zhtfrontend', '星期一');
                case 2:
                    return \Yii::t('zhtfrontend', '星期二');
                case 3:
                    return \Yii::t('zhtfrontend', '星期三');
                case 4:
                    return \Yii::t('zhtfrontend', '星期四');
                case 5:
                    return \Yii::t('zhtfrontend', '星期五');
                case 6:
                    return \Yii::t('zhtfrontend', '星期六');
                case 7:
                    return \Yii::t('zhtfrontend', '星期日');
            }
        };
        $fields['start_time'] = function () {
            $len = strlen($this->start_time) - 2;
            return floor($this->start_time / 100) . ':' . substr($this->start_time, $len);
        };
        $fields['end_time'] = function () {
            $len = strlen($this->end_time) - 2;
            return floor($this->end_time / 100) . ':' . substr($this->end_time, $len);
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }

    public function check_time($attribute){
        if(!empty($this->start_time) && !empty($this->end_time) ){
            $start_time = (int)str_replace(':', '', $this->start_time);
            $end_time = (int)str_replace(':', '', $this->end_time);
            if ($end_time <= $start_time) {
                $this->addError($attribute, Yii::t('zhtfrontend', '下课时间小于等于上课时间！'));
            }
//            if ($this->id != null) {
//                $datas = ClassroomAttr::find()->where(['classroom_id' => $this->classroom_id,'classday'=>$this->classday])->andWhere("id!=$this->id")->asArray()->all();
//                if ($datas != null) {
//                    foreach($datas as $data) {
//                        if ((($data['start_time'] <= $start_time && $data['end_time'] >= $start_time) || ($data['start_time'] <= $end_time && $data['end_time'] >= $end_time)) || (($data['start_time'] >= $start_time && $data['start_time'] <= $end_time) || ($data['end_time'] >= $start_time && $data['end_time'] < $end_time))) {
//                            $this->addError($attribute, Yii::t('zhtfrontend', '排课时间有冲突！'));
//                            break;
//                        }
//                    }
//                }
//            } else {
//                $datas = ClassroomAttr::find()->where(['classroom_id' => $this->classroom_id,'classday'=>$this->classday])->asArray()->all();
//                if ($datas != null) {
//                    foreach($datas as $data) {
//                        if ((($data['start_time'] <= $start_time && $data['end_time'] >= $start_time) || ($data['start_time'] <= $end_time && $data['end_time'] >= $end_time)) || (($data['start_time'] >= $start_time && $data['start_time'] <= $end_time) || ($data['end_time'] >= $start_time && $data['end_time'] < $end_time))) {
//                            $this->addError($attribute, Yii::t('zhtfrontend', '排课时间有冲突！'));
//                            break;
//                        }
//                    }
//                }
//            }
        }
    }


    public function getClassroom()
    {
        return $this->hasOne(Classroom::className(), ['id' => 'classroom_id']);
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'user_id']);
    }

    public function getSales()
    {
        return $this->hasMany(Sales::className(), ['classroom_attr_id' => 'id']);
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }
}
